package com.back.demo.service;

import com.back.demo.common.Permission;
import com.back.demo.mapper.BasicMapper;
import com.back.demo.common.User;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;

@org.springframework.stereotype.Service
public class BasicService {
    @Autowired
    private BasicMapper basicMapper;

    public ArrayList<Integer> findRoleIdByUsername(String username) {
        return basicMapper.selectRoleIdByUsername(username);
    }

    public User findUserByUsername(String username) {
        return basicMapper.selectUserByUsername(username);
    }

    public void register(User user) {
        basicMapper.register(user);
    }

    public ArrayList<Permission> findPermissionByUsername(String username) {
        return basicMapper.selectPermissionByUsername(username);
    }

    public User findInformationByUsername(String username) {
        return basicMapper.selectInformationByUsername(username);
    }

    public void changePassword(String username, String newPassword) {
        basicMapper.changePassword(username, newPassword);
    }

    public String getName(String username) {
        Integer userId = basicMapper.getUserId(username);
        Integer roleId = basicMapper.getRole(userId);
        if (roleId == null) {
            return "佚名";
        }
        switch (roleId) {
            case 1:
                return basicMapper.getStudentName(userId);
            case 2:
                return basicMapper.getJudgeName(userId);
            case 3:
                return basicMapper.getAdminName(userId);
        }
        return null;
    }

}
